processReminder
processReminder
-
Start Reminder Process
- Capture current time as
nowusingmoment().
- Capture current time as
-
Fetch All Open Rating Notifications
-
Query the
ratingNotificationRepofor all records:-
Where
status = OPEN -
Selects key fields:
id,type,cycleType,status,appointmentId,customerId,sentAt,isInternal
-
-
Result:
allOpenNotifications[]
-
-
Extract Unique Customer IDs
-
From
allOpenNotifications, extract and deduplicate:customerId→ ensure valid numbers
-
Result:
allCustomerIds[] -
Condition:
-
If
allCustomerIdsis empty:- Return early with
{ success: true, sent: 0, closed: 0, summary: [] }usinglogAndReturn()
- Return early with
-
-
-
Fetch Customer Notification Preferences
-
Query
notificationPreferenceRepowhere:customer.idinallCustomerIdsnotificationService.name = 'rating'notificationTypein[EMAIL, SMS]status = OPTED_OUT
-
Includes relations:
customer,notificationService -
Result:
customerPreferences[]
-
-
Build Opt-Out Map
-
Create
Map<number, Set<NotificationType>>:-
For each preference in
customerPreferences:- Add
notificationTypeto the correspondingcustomerIdin the map
- Add
-
-
Result:
optedOutMap
-
-
Filter Notifications
-
Call
filterNotifications(allOpenNotifications, optedOutMap, now):-
Purpose:
-
Filter out:
- Notifications due for sending
- Notifications to cancel
-
Based on:
- Opt-out settings (
optedOutMap) - Time elapsed since last sent (
now) - Notification cycle phase (
cycleType)
- Opt-out settings (
-
-
Result:
notificationsToSend[]: those eligible to be sentnotificationsToClose[]: those to be closed/cancelled
-
-
-
Fetch Related Appointments
-
Call
fetchAppointmentsForNotifications(notificationsToSend):- Retrieves appointments (internal or external) linked to each notification
- Returns a
Map<appointmentId, Appointment>
-
Result:
appointmentMap
-
-
Send Notifications & Build Summary
-
Call
sendNotificationsAndBuildSummary(notificationsToSend, appointmentMap):-
Sends actual notifications (email/SMS)
-
Builds records for update:
notificationsToUpdate[]: with newsentAt, updatedcycleTypesummaryMap: keyed summary of sent notifications
-
-
Result:
notificationsToUpdate[]summaryMap
-
-
Update Processed Notification Records
-
If
notificationsToUpdate.length > 0:- Increment Prometheus metric
ratingsTypeProcessedCounter.labels('reminder')by count
- Increment Prometheus metric
-
Batch Update Notification Statuses
-
For each item in
notificationsToUpdate[]:- Update
sentAtandcycleTypeusingratingNotificationRepo.update()
- Update
-
-
-
Close Expired Notifications
-
If
notificationsToClose.length > 0:- Update
status = CANCELLEDwhereid in notificationsToClose
- Update
-
-
Return Process Summary
-
Use
logAndReturn()to return and log the result:{
success: true,
sent: <number of notifications sent>,
closed: <number of notifications closed>,
summary: <array of summaryMap values>,
}
-